import React, { Component } from 'react';

import PropTypes from 'prop-types'
import { bold } from 'ansi-colors';


class TableInfo extends Component {
    constructor(props) {
        super(props);
        this.state = {
            beginDate: this.props.beginDate, //开始时间
            dayOps: this.props.dayOps, //产后/术后天数
            dayList: this.props.dayList //住院天数
        }
    }


    //设置props值类型
    static propTypes = {
        beginDate: PropTypes.string.isRequired,
        dayOps: PropTypes.array,
        dayList: PropTypes.array,
    }

    //初始化默认值
    static defaultProps = {

        beginDate: getNewDate(new Date()),
        dayList: [],
        dayOps: [],

    }

    componentWillReceiveProps(nextProps, nextState) {
        /*
         * 开始时间beginDate改变，则重新render
         * */
        if (nextProps.beginDate !== nextState.beginDate) {
            this.setState({
                beginDate: nextProps.beginDate, //开始时间
                dayOps: nextProps.dayOps, //产后/术后天数
                dayList: nextProps.dayList //住院天数
            })
        }
    }

    /*
     * @method getDaysArray 生成显示天数
     * @param {String} beginDate 开始时间
     * */
    getDaysArray = (beginDate) => {
        const showDays = 21;
        //根据当前日期和显示天数生成日期数组
        let daysArray = [];
        for (let i = 0; i < showDays; i++) {
            let newDate = getNewDate(beginDate, i);
            daysArray.push(newDate);
        }
        return daysArray;
    }

    /*
     * @method getTimes 生成每天的时刻 2-6-10-14-18-22
     * */
    getTimes = () => {
        //生成timesArray
        let timesArray = [];
        for (let i = 0; i < 42; i++) {
            let num = i % 2 === 1 ? 14 : 6;
            let td = <td key={i}>{num}</td>;
            if (num === 14) {
                td = <td key={i} className="borderR">{num}</td>;
            }
            timesArray.push(td);
        }
        return timesArray;
    }

    _dayOpsStr(opt) {
        if (opt === -1) {
            return ''
        }
        else if (opt === 0) {
            return "手术"
        }
        else {
            return opt || '';
        }

    }

    inHospitalDays(opt) {
        if (opt > 0) {
            return opt;
        } else {
            return ''
        }
    }

    render() {

        let { beginDate, dayOps, dayList } = this.state;
        let daysArray = this.getDaysArray(beginDate);
        let timesArray = this.getTimes();

        return (
            <tbody>
                <tr id="dateTR">
                    <td>日期</td>
                    {daysArray.map((v, i) => {
                        return <td key={i} colSpan={2} className="borderR">{v}</td>
                    })}
                    <td />
                </tr>

                <tr id="hospDaysTr">
                    <td>住院天数</td>
                    {daysArray.map((v, i) => {
                        return <td key={i} colSpan={2} className="borderR">{this.inHospitalDays(dayList[i])}</td>
                    })}
                    <td />
                </tr>

                {/* <tr id="operaDaysTr">
                    <td>术后产后天数</td>
                    {daysArray.map((v, i) => {
                        return <td key={i} colSpan={2} className="borderR textRed">{this._dayOpsStr(dayOps[i])}</td>
                    })}
                    <td />
                </tr> */}

                <tr id="timeTr">
                    <td>时间</td>
                    {timesArray.map((v, i) => {
                        return v
                    })}
                    <td />
                </tr>

            </tbody>
        );
    }
}

export default TableInfo;


/**
     * @method getNewDate 根据传入时间，获取新的时间
     * @param {Object} dateStr 传入时间
     * @param {Number} dis 改变规则 ，-1为前一天，1为后一天，依次类推
     */
function getNewDate(dateStr, dis) {
    //判断是否Date()对象，如果不是转为Date对象
    let newDate;
    if (dateStr instanceof Date) {
        newDate = dateStr;
    } else {
        //将字符串时间转化成Date对象
        newDate = new Date(dateStr.replace(/-/g, "/"));
    }

    let type = dis || 0;
    //判断是否是合法的时间对象
    let dateParse = Date.parse(newDate);
    if (isNaN(dateParse)) { //日期是否合法
        console.log("日期格式不合法");
        return; //不合法时返回false
    }
    let year = newDate.getFullYear();
    let month = newDate.getMonth();
    let day = newDate.getDate();
    let finalDate = new Date(year, month, day + type);
    let finalYear = finalDate.getFullYear();
    let finalMonth = finalDate.getMonth() < 9 ? "0" + (finalDate.getMonth() + 1) : (finalDate.getMonth() + 1);
    let finalDay = finalDate.getDate() < 10 ? "0" + finalDate.getDate() : finalDate.getDate();

    let isNewMonth = finalDay === "0" + 1; // 新的一月
    let isNewYear = finalMonth === "0" + 1 && isNewMonth

    if (dis === 0 || isNewYear) {
        return finalYear + "-" + finalMonth + "-" + finalDay;
    }
    else if (isNewMonth) {
        return finalMonth + "-" + finalDay;

    } else {
        return finalDay;
    }



}


